package lurker

import (
	"go-lurker/lurker/api"
	"log"
)

type LurkerItem struct {
	Url      string
	StartTag string
	EndTag   string
}

type GoLurker struct {
	items        []*LurkerItem
	writeFileLog bool
}

func NewGoLurker() *GoLurker {
	return &GoLurker{}
}
func (_this *GoLurker) SetItem(item *LurkerItem) *GoLurker {
	if item == nil {
		log.Fatal("item is nil")
		return nil
	}
	_this.items = append(_this.items, item)
	return _this
}

func (_this *GoLurker) SetWriteFileLog(isWrite bool) *GoLurker {
	_this.writeFileLog = isWrite
	return _this
}

func (_this *GoLurker) Run(callback func(data string)) {
	if callback == nil {
		log.Fatal("callback is nil")
		return
	}
	if len(_this.items) == 0 {
		log.Fatal("items length is invalid : zero")
		return
	}
	master := api.NewMaster(len(_this.items))
	for _, item := range _this.items {
		func(_item *LurkerItem) {
			master.AddJob(func() {
				data := FetchData(_item.Url)
				if data == "" {
					log.Fatal("no found any data")
				} else {
					hc := &HtmlContent{
						Html:     data,
						StartTag: item.StartTag,
						EndTag:   item.EndTag,
					}
					d := ParseHTMLContent(hc)
					if _this.writeFileLog {
						StoreToFile("log_data.txt", d)
					}
					callback(d)
				}
			})
		}(item)
	}
	master.Run()
}

func CreateLurker() *GoLurker {
	return NewGoLurker()
}
